package core;

import branch.Branch;
import fileoperation.FileCreation;
import fileoperation.FileDeletion;
import gitobject.Blob;
import gitobject.Commit;
import gitobject.Tree;
import repository.Repository;
import stage.Index;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class JitReset {
    /**
     * according to the mode reset to specified commit
     */
    public static void reset(String mode, String CommitId) throws IOException {
        Commit commit = Commit.deserialize(CommitId);
        if (mode.equals("soft"))
            resetSoft(commit);
        if (mode.equals("mixed"))
            resetMixed(commit);
        if (mode.equals("hard")) {
            resetHard(commit);
        }
    }

    public static void resetSoft(Commit commit) throws IOException {

//        File HEAD = new File(Repository.getGitDir() + File.separator + "HEAD");
//        FileOutputStream fos = new FileOutputStream(HEAD);
//        byte[] keyContent = commit.getKey().getBytes();
//        fos.write(keyContent);
//        fos.close();
        String current_branch = Branch.getCurrentBranchName();
        FileCreation.createFile(Branch.path, current_branch, commit.getKey());
    }

    public static void resetMixed(Commit commit) throws IOException {
        Index index = new Index();
        String current_branch = Branch.getCurrentBranchName();
        FileCreation.createFile(Branch.path, current_branch, commit.getKey());
        Tree tree = Tree.deserialize(commit.getTree());
        index.reset(tree);
    }

    public static void resetHard(Commit commit) throws IOException {
        String current_branch = Branch.getCurrentBranchName();
        FileCreation.createFile(Branch.path, current_branch, commit.getKey());
        Index index = new Index();
        Tree tree = Tree.deserialize(commit.getTree());
        index.reset(tree);
        File workTree = new File(Repository.getWorkTree());
        File[] dir = workTree.listFiles();
        assert dir != null;
        for (File file : dir) {
            if (!file.getName().equals(".jit"))
                FileDeletion.deleteAll(file);
        }
        index.getInfo().forEach((name, entry) -> {
            try {
                createFile(Repository.getWorkTree() + File.separator + name, entry[1]);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * reset the working area
     */
    public static void createFile(String path, String hash) throws IOException {
        Blob blob = Blob.deserialize(hash);
        String[] paths = path.split("[\\\\|/]");
        String parentPath = paths[0];
        for (int i = 1; i < paths.length - 1; i++) {
            parentPath += File.separator + paths[i];
        }
        String name = paths[paths.length - 1];
        String content = blob.getValue();
        File parent = new File(parentPath);
        if (!parent.exists())
            parent.mkdirs();
        FileCreation.createFile(parentPath, name, content);
    }
}